//
//  GasSpendListVC.m
//  NtoccDriverNew
//
//  Created by niyongsheng on 2022/10/18.
//  Copyright © 2022 GCY. All rights reserved.
//

#import "GasSpendListVC.h"
#import "GasSpendListCell.h"

@interface GasSpendListVC ()
<
UITableViewDelegate,
UITableViewDataSource
>
{
    NSInteger _pageNum;
    NSString *_keyword;
    
    NSString *_startTime;
    NSString *_endTime;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSourceMArr;

@property (weak, nonatomic) IBOutlet UIView *searchView;
@property (weak, nonatomic) IBOutlet UITextField *searchTF;

@property (weak, nonatomic) IBOutlet UIButton *startTimeBtn;
@property (weak, nonatomic) IBOutlet UIButton *endTimeBtn;
@property (weak, nonatomic) IBOutlet UIButton *clearBtn;
@end

@implementation GasSpendListVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"消费记录";
    
    ViewRadius(_searchView, 5)
    [self.view addSubview:self.tableView];
    
    [self.tableView.mj_header beginRefreshing];
}

- (IBAction)searchBtnOnclicked:(UIButton *)sender {
    [self.view endEditing:YES];
    
    _keyword = _searchTF.text;
    [self.tableView.mj_header beginRefreshing];
}

/// 刷新
/// @param isHeader 是否下拉刷新
- (void)refreshing:(BOOL)isHeader {
    if (isHeader) {
        _pageNum  = 1;
    } else {
        _pageNum ++;
    }
    NSString *urlStr = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tps-api",[ReAddressDynamic shared].baseUrl],GasConsumptionRecord);
    NSMutableDictionary *params = @{
        @"transactionStatus" : @"2,3",
        @"pageNum" : @(_pageNum),
        @"pageSize" : @10,
        @"energyCardType" : self.typeStr
    }.mutableCopy;
    [params setValue:[UserDefaultUtil valueForKey:DriverPartyCode] forKey:@"partyCode"];
    // 搜索条件
    [params setValue:_keyword forKey:@"tradeNo"];
    [params setValue:_startTime forKey:@"gmtCreatedStart"];
    [params setValue:_endTime forKey:@"gmtCreatedEnd"];
    
    @weakify(self)
    [HTTPManager getNetworkRequest:urlStr
                     withParameter:params
                            withVC:self
                           netName:@"消费记录"
                           success:^(id response, NSString *msg) {
        @strongify(self)
        if (isHeader) {
            self.dataSourceMArr = [GasSpendListModel mj_objectArrayWithKeyValuesArray:response[@"records"]];
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
            
        } else {
            NSArray *dictArr = response[@"records"];
            if (dictArr.count > 0) {
                [self.dataSourceMArr addObjectsFromArray:[GasSpendListModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
                [self.tableView.mj_footer endRefreshing];
                [self.tableView reloadData];
                
            } else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
    } failure:^(NSInteger code, NSString *msg) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - TableView 代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceMArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellReuseIdentifier = @"GasSpendListCell";
    GasSpendListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSourceMArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, ScreenW, ScreenH - MenuBarHeight - NavHeight - 100) style:UITableViewStylePlain];
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([GasSpendListCell class]) bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:NSStringFromClass([GasSpendListCell class])];
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        @weakify(self)
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self)
            [self refreshing:YES];
        }];
        header.lastUpdatedTimeLabel.hidden = YES;
        MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
            @strongify(self)
            [self refreshing:NO];
        }];
        
        _tableView.mj_header = header;
        _tableView.mj_footer = footer;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (IBAction)startTimeBtnOnclicked:(UIButton *)sender {
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMD];
    datePickerView.title = @"选择开始日期";
    datePickerView.isAutoSelect = NO;
    datePickerView.showToday = NO;
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        _startTime = [selectValue stringByAppendingFormat:@" 00:00:00"];
        [sender setTitle:selectValue forState:UIControlStateNormal];
        if ([_startTime isNotBlank] && [_endTime isNotBlank]) {
            [self.tableView.mj_header beginRefreshing];
        }
    };
    [datePickerView show:nil];
}

- (IBAction)endTimeBtnOnclicked:(UIButton *)sender {
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMD];
    datePickerView.title = @"选择结束日期";
    datePickerView.isAutoSelect = NO;
    datePickerView.showToday = NO;
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        _endTime = [selectValue stringByAppendingFormat:@" 23:59:59"];
        [sender setTitle:selectValue forState:UIControlStateNormal];
        if ([_startTime isNotBlank] && [_endTime isNotBlank]) {
            [self.tableView.mj_header beginRefreshing];
        }
    };
    [datePickerView show:nil];
}

- (IBAction)clearBtnOnclicked:(UIButton *)sender {
    [self.startTimeBtn setTitle:@"开始时间" forState:UIControlStateNormal];
    [self.endTimeBtn setTitle:@"结束时间" forState:UIControlStateNormal];
    _startTime = nil;
    _endTime = nil;
    [self.tableView.mj_header beginRefreshing];
}

@end

